/*
 * CharacterModel.cpp
 *
 *  Created on: 20.10.2012
 *      Author: caffeine
 */




#include "CharacterModel.h"

void CharacterModel::resetAnimationForAllModel(){
	if (!animation)
		return;
	animation->useFor(body->getAnimationModel());
	animation->useFor(feet->getAnimationModel());
	animation->useFor(hands->getAnimationModel());
	animation->useFor(lags->getAnimationModel());
	animation->useFor(adds->getAnimationModel());
}

void CharacterModel::resetAnimationGroupForAllModel(){
	if (!animation_group)
		return;
	animation_group->useAnimation(body->getAnimationModel());
	animation_group->useAnimation(feet->getAnimationModel());
	animation_group->useAnimation(hands->getAnimationModel());
	animation_group->useAnimation(lags->getAnimationModel());
	animation_group->useAnimation(adds->getAnimationModel());
}

void CharacterModel::setAnimationGroup(irr::animation::IAnimation*group){
	if (animation_group)
		animation_group->drop();
	animation_group=group;
	if (animation_group){
		animation_group->grab();
		if (isLoaded()){
			resetAnimationGroupForAllModel();
			resetAnimationForAllModel();
		}
	}else
		animation_group=0;
}

irr::animation::IAnimation*CharacterModel::getAnimationGroup(){
	return animation_group;
}

void CharacterModel::setAnimation(irr::animation::IAnimationRecord*a){
	if (animation)
		animation->drop();
	animation=a;
	if (animation){
		animation->grab();
		if (isLoaded())
			resetAnimationForAllModel();
	}else
		animation=0;
}

void CharacterModel::setVisual(CharacterSets cs){
	facial=cs.get(CharacterPattern::FACIAL);
	hair=cs.get(CharacterPattern::HAIR);
	hair_head=cs.get(CharacterPattern::HAIR_HEAD);
	skin=cs.get(CharacterPattern::SKIN);
	face=cs.get(CharacterPattern::FACE);
	bodyIndex=cs.get(CharacterPattern::BODY);
	parent=cs.parent;
	feetIndex=cs.get(CharacterPattern::FEET);
	handsIndex=cs.get(CharacterPattern::HANDS);
	lagsIndex=cs.get(CharacterPattern::LAGS);
	addsIndex=cs.get(CharacterPattern::ADD);
	body->setFileName(parent->getBodys()->getHref(bodyIndex));
	feet->setFileName(parent->getFeet()->getHref(feetIndex));
	hands->setFileName(parent->getHands()->getHref(handsIndex));
	lags->setFileName(parent->getLags()->getHref(lagsIndex));
	adds->setFileName(parent->getAdds()->getHref(addsIndex));
	skin_layout->setPath(parent->getskins()->getHref(skin));
	facial_layout->setPath(parent->getFacial()->getHref(facial));
	face_layout->setPath(parent->getFace()->getHref(face));
}

CharacterSets CharacterModel::getVisual(){
	CharacterSets res(parent);

	res.set(CharacterPattern::FACIAL,facial);
	res.set(CharacterPattern::HAIR,hair);
	res.set(CharacterPattern::HAIR_HEAD,hair_head);
	res.set(CharacterPattern::SKIN,skin);
	res.set(CharacterPattern::FACE,face);
	res.set(CharacterPattern::BODY,bodyIndex);
	res.parent=parent;
	res.set(CharacterPattern::FEET,feetIndex);
	res.set(CharacterPattern::HANDS,handsIndex);
	res.set(CharacterPattern::LAGS,lagsIndex);
	res.set(CharacterPattern::ADD,addsIndex);
	return res;
}

void CharacterModel::setLayoutTexture(int type,irr::io::path path){
	switch (type){
	case PANTS:
		pants->setPath(path);
		break;
	case JACKET:
		jacket->setPath(path);
		break;
	case BOOTS:
		boots->setPath(path);
		break;
	}
}
